
local hard_list = load_excel('数据投放.xlsx','难度系数')
local unit_data = load_excel('数据投放.xlsx','怪物数据')

for k,v in pairs(unit_data) do
    v.name = k
    v.skill = v['技能'] and split_string(v['技能'],',')
    v.types = v['类型'] and split_string(v['类型'],',')
    ac.lni('unit',k,v)
end

local data_list = {}

local function init_data_list(data)
    data_list = {}
    for a=1,6 do
        local sc1 = data['输出'..(a-1)] or 100
        local sm1 = data['生命'..(a-1)] or 1500
        local hx1 = data['恢复'..(a-1)] or 75
        local sp1 = data['倍率'..(a-1)] or 1
        local js1 = data['减少'..(a-1)] or 1
        
        local sc2 = data['输出'..a] or sc1
        local sm2 = data['生命'..a] or sm1
        local hx2 = data['恢复'..a] or hx1
        local sp2 = data['倍率'..a] or sp1
        local js2 = data['减伤'..a] or js1

        local bd = data['波动'..a]
        if bd then
            bd = split_string(bd,'-')
        end
        local load1 = 0
        for b=1,6 do
            local load2 = bd and bd[b] and tonumber(bd[b])/100 or b/6
            for c=1,60 do
                local load3 = load1 + (load2 - load1)*c/60
                local sc3 = sc1 + (sc2-sc1)*load3
                local sm3 = sm1 + (sm2-sm1)*load3
                local hx3 = hx1 + (hx2-hx1)*load3
                local sp3 = sp1 + (sp2-sp1)*load3
                local js3 = js1 + (js2-js1)*load3
                data_list[#data_list + 1] = {
                    ['攻击'] = sc3,
                    ['生命'] = sm3,
                    ['恢复'] = hx3,
                    ['倍率'] = sp3,
                    ['减伤'] = js3,
                }
            end
            load1 = load2
        end
    end

end

local hard_data = hard_list[1]
init_data_list(hard_data)

ac.game:event '游戏-设置难度'(function(_,hard)
    hard_data = hard_list[hard] or hard_list[1]
    init_data_list(hard_data)
    for u in ac.selector('unit')
        : range(ac.point(0,0),20000)
        : can_god()
        : loop()
    do
        if u['默认等级'] then
            u:set_enemy_level(u['默认等级'])
        end
    end
end)

ac.game:event '单位-初始化'(function(_,unit)
    if unit['默认等级'] then
        unit:set_enemy_level(unit['默认等级'])
    end
end)

local params = load_excel('数据投放.xlsx','挑战等级')

function ac.unit.__index:set_enemy_level(level,name)
    local name = name or self.name
    local data1 = unit_data[name]
    local level = level or data1['默认等级']
    local data2 = data_list[level]
    if not data1 then
        log.error('错误怪物类型',name)
    end
    if not data2 then
        log.error('错误怪物等级',level)
        return
    end
    local tlv = math.ceil(level/60)
    local hj = data1['护甲'] or ac.enemy and params[tlv] and params[tlv]['流程护甲'] or 0
    local time = data1['击杀时间'] or 1
    local rate = data1['每秒输出'] or -20
    local block = data1['消耗抵制'] or 0

    local sm = data2['生命']
    local hx = data2['恢复']
    local gj = data2['攻击']
    local sp = data2['倍率'] or 0
    local js = data2['减伤'] or 0
    local zdl = gj * sp
    if ac['自适应数据'] then
        sm = ac['自适应数据']['生命']
        hx = ac['自适应数据']['恢复']
        zdl = ac['自适应数据']['攻击']
        js = ac['自适应数据']['减伤']
        zdl = zdl * 2
    end
    
    local zgj = zdl
    local dps = sm * rate / 100 + hx

    if rate<0 then
        dps = - hx * rate / 100
    end
    
    if data1['攻击距离'] then
        self:set('攻击距离',data1['攻击距离'])
    end

    local gap = data1['攻击间隔'] or 1
    local atk = dps * gap * (1 + js/100)
    local hxz = zgj * block/(block + 100)
    local smz = (zgj - hxz)*time
    self.enemy_level = level
    self.zdl = zdl
    self:set('攻击间隔',gap)
    self:set('基础攻击',atk)
    self:set('基础护甲',hj)
    self:set('生命上限',smz)
    self:set('生命恢复',hxz)
    self:add_type(data1.types)

    if (self:is_type('精英') or self:is_type('挑战') or data1['类型']=='挑战') and unit_add_life2 then
        unit_add_life2(self)
        self:add_effect([[Abilities\Spells\Items\AIil\AIilTarget.mdl]]):remove()
    elseif self:is_type('领主') and unit_add_life2 then
        unit_add_life2(self,2)
        self:add_effect([[Abilities\Spells\Items\AIil\AIilTarget.mdl]]):remove()
    end
    return self
end


ac.buff('动态测试数据'){
    pulse = 1,
    on_add = function(self)
        -- local unit = self.owner
        -- self:gc(ac.game:event '测试-玩家数据变动'(function()
        --     if unit.not_update==nil then
        --         unit:set_enemy_level(1)
        --     end
        -- end))
    end,
}

local keys = {
'基础攻击','生命上限','生命恢复','基础护甲','伤害减免','基础力量','基础敏捷','基础智力','攻击速度','攻击间隔',
'力量加成','敏捷加成','智力加成','攻击加成','装备加成','生命加成','生命恢复加成','物理固伤加成','魔法固伤加成',
'暴击伤害','物理伤害','魔法伤害','伤害增幅','最终伤害','对进攻怪增伤','对挑战怪增伤','对BOSS增伤',
'每秒金币','每秒木材','经验加成','杀敌加成','多重数量','弹射次数','多重伤害','弹射伤害',
}

local test_zdl = function(hero)
    local ex,dmg = get_ex_attack(hero  ,damage_type)
    local dps = (hero:get('攻击') + ex) * (1 + dmg*0.01) * math.min(hero:get('攻击速度')*0.01,5) * (1 + hero:get('暴击几率')*0.01*(hero:get('暴击伤害')*0.01 - 1))
    local smz = hero:get('生命上限')
    local hxz = hero:get('生命恢复')
    local js = (1 + hero:get('基础护甲')*0.01) * (1 + hero:get('伤害减免')*0.01) * 100
    dps = dps * (1 + hero:get('伤害增幅')*0.01)
    dps = dps * (1 + hero:get('最终伤害')*0.01)
    dps = dps * (1 + hero:get('对BOSS增伤')*0.01)
    return math.ceil(dps),math.ceil(smz),math.ceil(hxz),math.ceil(js)
end

local json = require 'util.json'

ac.game:event '游戏-怪物数据测试'(function(trg)
    trg:remove()

    local path1 = [[C:\Users\99\Desktop\阶段数据.txt]]
    local path2 = [[C:\Users\Administrator\Desktop\阶段数据.txt]]
    local file2 = io.open(path2, "w")
    if file2==nil then
        path2 = path1
    else
        file2:close()
    end
    local file2 = io.open(path2, "w")
    file2:write('')
    file2:close()
    --每隔5秒获取最高战力
    local max_atk = 0
    local max_sxz = 0
    local max_hxz = 0
    local max_js = 0
    ac.loop(5*1000,function()
        local unit = nil
        local open = false
        ac.each_player(function(player)
            local hero = player.hero
            if hero then
                unit = hero
                local dps,smz,hxz,js = test_zdl(hero)
                if dps>max_atk*1.1 then
                    open = true
                    max_atk = math.ceil(dps)
                end
                if smz>max_sxz*1.1 then
                    open = true
                    max_sxz = math.ceil(smz)
                end
                if hxz>max_hxz*1.1 then
                    open = true
                    max_hxz = math.ceil(hxz)
                end
                if js>max_js*1.1 then
                    open = true
                    max_js = math.ceil(js)
                end
            end
        end)
        -- ac['自适应数据'] = {
        --     ['攻击'] = max_atk,
        --     ['生命'] = max_sxz,
        --     ['恢复'] = max_hxz,
        --     ['减伤'] = max_js,
        -- }
        if open then
            ac.game:notify('测试-玩家数据变动')
        end
        local file2 = io.open(path2, "a")
        local info = {max_atk,max_sxz,max_hxz,max_js}
        for _,key in ipairs(keys) do
            info[#info + 1] = math.ceil(unit:get(key))
        end
        local str2 = table.concat(info,',') .. '\n'
        file2:write(str2)
        file2:close()
    end)
end)

register_hero(function()
    ac.game:notify('游戏-怪物数据测试')
end)


local lock = {
    ['基础力量'] = true,
    ['基础敏捷'] = true,
    ['基础智力'] = true
}
local path1 = [[C:\Users\99\Desktop\游戏快照.txt]]
local path2 = [[C:\Users\Administrator\Desktop\游戏快照.txt]]
local file2 = io.open(path2, "r")
if file2==nil then
    path2 = path1
else
    file2:close()
end

local save_info = {}
local save_data = function(player)
    local unit = player.hero
    local info = {}
    local dps,smz,hxz,js = test_zdl(unit)
    print('===========投放参考============')
    print(dps,smz,hxz,js)
    unit:remove_buffs()
    for _,key in ipairs(keys) do
        info[key] = math.ceil(unit:get(key))
    end

    local its = {}
    for it in ac.selector('item')
        : rect(ac.rect.map)
        : loop()
    do
        if it.owner_player==player then
            its[#its + 1] = it.name
        end
    end

    local item = {}
    for _,it in ipairs(unit:get_items()) do
        item[#item + 1] = it.name
    end
    local tf = {}
    for _,temp in ipairs(player:get_data('天赋显示表') or {}) do
        tf[#tf + 1] = temp.name
    end
    
    local ts = {}
    for _,temp in ipairs(player:get_data('装备吞噬列表') or {}) do
        ts[#ts + 1] = temp.name
    end
    
    local tz = {}
    for _,temp in ipairs(player:get_data('挑战技能表') or {}) do
        tz[#tz + 1] = {count = temp.count,stack = temp.stack,cd = temp:get_charge_cd()}
    end

    local sp = {}
    local sp_lv = {}
    for a=1,7 do
        local skill = unit:find_skill(a,'英雄')
        if skill then
            sp[#sp + 1] = skill.name
            sp_lv[#sp_lv + 1] = skill.level
        end
    end

    local save = {
        sx = info,
        it = item,
        lv = unit.level,
        tf = tf,
        ts = ts,
        sp = sp,
        sp_lv = sp_lv,
        tz = tz,
        its = its,
        gold = player:get('金币'),
        lum = player:get('木材'),
        zs = unit:get_data('转生次数',0),
    }
    
    local file2 = io.open(path2, "w")
    local str2 = json.encode(save)
    file2:write(str2)
    file2:close()
    
    unit:add_buff '弹道设置'{}
end

ac.game:event '玩家-聊天'(function(_,player,str)
    if str=='-save' then
    end
    if str=='-load' then
        local unit = player.hero
        local file2 = io.open(path2, "r")
        if file2 then
            local str = file2:read('*a')
            local info = json.decode(str)
            file2:close()
            unit:set_level(tonumber(info.lv) or 1)
            unit:set_data('转生次数',info.zs)
            for _,name in ipairs(info.it or {}) do
                unit:add_item(name)
            end
            for _,name in ipairs(info.tf or {}) do
                unit:notify('天赋-获得属性',unit,ac.tf_list[name])
            end
            for _,name in ipairs(info.ts or {}) do
                local item = ac.item.create(name,ac.point(0,0))
                unit:notify('单位-吞噬物品', unit, item)
            end
            for _,name in ipairs(info.its or {}) do
                local item = ac.item.create(name,ac.point(0,0))
                collate_item(unit,item)
            end
            for a,temp in ipairs(player:get_data('挑战技能表') or {}) do
                local da = info.tz and info.tz[a]
                temp.count = da.count
                if da.stack and da.stack>0 then
                    temp:set_stack(da.stack)
                end
                if da.cd and da.cd>0 then
                    temp:set_cd(da.cd)
                end
            end
            for a,name in ipairs(info.sp or {}) do
                local skill = unit:add_skill(name,'英雄')
                skill:set_level(info.sp_lv[a] or 1)
            end
            player:set('金币',info.gold or 0)
            player:set('木材',info.lum or 0)
            local sx = info.sx
            for key in pairs(lock) do
                unit:set(key,sx[key] or 0)
            end
            for key,num in pairs(sx) do
                if lock[key]==nil then
                    unit:set(key,tonumber(num))
                end
            end
        end
    end
end)